﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using eCommMongo.Core;
using eCommMongo.Core.Data;
using eCommMongo.Core.Domain.Customers;
using eCommMongo.Core.Domain.Pictures;
using eCommMongo.Core.Domain.Posts;
using eCommMongo.Core.Domain.Products;
using eCommMongo.Core.Domain.Tags;
using eCommMongo.Service.Customers;
using eCommMongo.Service.Pictures;

namespace eCommMongo.Service.Posts
{
    public class PostMapper
    {
        public static IList<Post> ToPostList(IList<Post> posts)
        {
            if (posts == null)
            {
                return new List<Post>();
            }
            else
            {
                return posts.Select(c => ToPostModel(c)).ToList();
            }
        }


        public static Post ToPostModel(Post post)
        {
            var custService = EngineContext.Current.Resolve<ICustomerService>();
            var prodRep = EngineContext.Current.Resolve<IMongoRepository<Product>>(); 
            var tagRep = EngineContext.Current.Resolve<IMongoRepository<Tag>>();
            var picService= EngineContext.Current.Resolve<IPictureService>();
            if (post == null)
            {
                return null;
            }
            else
            {
                post.Customer = custService.GetCustomerById(post.Customer.Id);
               
                if (post.IsProduct)
                {
                    post.Product = prodRep.GetById(post.Product.Id);
                }

                if (post.Tags != null)
                {
                    foreach (var postTag in post.Tags)
                    {
                        var tagObj = tagRep.GetById(postTag.Tag.Id);
                        postTag.Tag = tagObj;
                    }
                }
                if (post.PostPictures != null)
                {
                    foreach (var pp in post.PostPictures)
                    {
                        var pic = picService.GetPictureById(pp.Picture.Id);
                        pp.Picture.PicUrl = picService.GetPictureUrl(pic, 80, true);
                    }
                }
                post.CreatedAtStr = post.CreatedAt.Ticks.ToString();
                return post;
               
            }
        }

     

        //private static ICollection<StatusTag> ToStatusTags(ICollection<StatusTag> statusTags)
        //{
        //    return statusTags.Select(c => new StatusTag()
        //        {
        //            Tag = new Tag()
        //                {
        //                    TagName = c.Tag.TagName,
        //                    TagValue = c.Tag.TagValue
        //                }
        //        }).ToList();
        //}

        public static ICollection<Comment> ToComments(ICollection<Comment> comments)
        {
            return comments.Select(c => ToComment(c)).ToList();
        }

       
        public static Comment ToComment(Comment comment)
        {
            if (comment == null)
                return null;
            else
            {
                var custService = EngineContext.Current.Resolve<ICustomerService>();
                comment.Commenter = custService.GetCustomerById(comment.Commenter.Id);

               
                return comment;

            }
        }

        //private static ICollection<StatusPicture> ToStatusPictures(ICollection<StatusPicture> statusPictures)
        //{
        //    if (statusPictures == null)
        //    {
        //        return null;
        //    }
        //    else
        //    {
        //        return statusPictures.Select(c => new StatusPicture()
        //            {
        //                PictureId = c.PictureId
        //            }).ToList();
        //    }
        //}


        public static IList<Comment> ToCommentList(List<Comment> toList)
        {
            throw new NotImplementedException();
        }

        //public static IList<Tag> ToTagList(List<Tag> tags)
        //{
        //    if (tags == null)
        //    {
        //        return null;
        //    }
        //    else
        //    {
        //        return tags.Select(c => new Tag()
        //            {
        //                CountTaged = c.CountTaged,
        //                TagName = c.TagName,
        //                TagValue = c.TagValue,
        //                CreateAt = c.CreateAt,
        //                CreatorId = c.CreatorId,
        //                Deleted = c.Deleted

        //            }).ToList();
        //    }
        //}
    }
}